package com.gdziarmaga.webshop.web.menus;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.springframework.beans.factory.annotation.Autowired;

import com.gdziarmaga.webshop.transaction.model.Transaction;
import com.gdziarmaga.webshop.transaction.model.TransactionBook;
import com.gdziarmaga.webshop.transaction.services.TransactionManager;
import com.gdziarmaga.webshop.utils.filtering.BaseFilter;
import com.gdziarmaga.webshop.utils.filtering.FilterComparisonType;
import com.gdziarmaga.webshop.utils.filtering.FilterField;
import com.gdziarmaga.webshop.utils.springcontext.AppContext;
import com.gdziarmaga.webshop.web.utils.FacesUtils;

@ManagedBean(name = "transactionsBean")
@RequestScoped
public class TransactionsBean {

	@Autowired
	private TransactionManager transactionManager;

	@PostConstruct
	public void init() {
		transactionManager = (TransactionManager) AppContext
				.getApplicationContext().getBean("transManager");
	}

	private Transaction transaction;
	private List<TransactionBook> transactionBooks;

	public List<TransactionBook> getTransactionBooks() {
		if (transactionBooks == null) {
			transactionBooks = new ArrayList<TransactionBook>();
		}
		return transactionBooks;
	}

	public void setTransactionBooks(List<TransactionBook> transactionBooks) {
		this.transactionBooks = transactionBooks;
	}

	public void removeTransaction() {
		AdminPanelBean bean = ((AdminPanelBean) FacesUtils.getRequest()
				.getSession().getAttribute("adminPanelBean"));
		Long id = ((Transaction) bean.getSelectionItems().get(0))
				.getTransaction_id();
		if (id != null) {
			transactionManager.deleteTransaction(id);
		}
	}

	public void markAsDone() {
		AdminPanelBean bean = ((AdminPanelBean) FacesUtils.getRequest()
				.getSession().getAttribute("adminPanelBean"));
		Long id = ((Transaction) bean.getSelectionItems().get(0))
				.getTransaction_id();
		if (id != null) {
			Transaction transaction = transactionManager.getTransactionById(id);
			transaction.setRealised(1);
			transactionManager.saveTransaction(transaction);
			bean.refreshTransactionList();
		}
	}

	public boolean showTransaction() {
		AdminPanelBean bean = ((AdminPanelBean) FacesUtils.getRequest()
				.getSession().getAttribute("adminPanelBean"));
		if (!bean.getSelectionItems().isEmpty()) {
			if (bean.getSelectionItems().get(0) instanceof Transaction) {
				Long id = ((Transaction) bean.getSelectionItems().get(0))
						.getTransaction_id();
				transaction = transactionManager.getTransactionById(id);

				BaseFilter filter = new BaseFilter();
				filter.addFilterField(new FilterField<Transaction>(transaction,
						"transaction", FilterComparisonType.EQUAL));
				transactionBooks = transactionManager
						.getTransactionBookByFilter(filter);
				return true;
			}
		}
		return false;
	}

	public Transaction getTransaction() {
		if (transaction == null) {
			if (!showTransaction()) {
				transaction = new Transaction();
			}
		}
		return transaction;
	}

	public void setTransaction(Transaction transaction) {
		this.transaction = transaction;
	}

	public float getPrice() {
		float price = 0;
		for (TransactionBook transactionBook : getTransactionBooks()) {
			price += transactionBook.getBought_books()
					* transactionBook.getBook().getPrice();
		}
		return price;
	}
}
